import 'dart:async';

import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'xin_he_flash_sale_goods_detail_state.dart';

class XinHeFlashSaleGoodsDetailLogic extends GetxController {
  final XinHeFlashSaleGoodsDetailState state = XinHeFlashSaleGoodsDetailState();
  ScrollController scrollController = ScrollController();
  late Offset pointerStart;
  late Offset pointerEnd;
  double touchRangeX = 0;
  double touchRangeY = 0;
  double nextOffset = 0;
  int lastPage = 0;

  ///限时抢购
  Timer? _snapUpTimer;
  int secondsRemaining = 3610; // 倒计时的总秒数
  RxString snapUpHour = '00'.obs;
  RxString snapUpMinute = '00'.obs;
  RxString snapUpSecond = '00'.obs;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    startCountdown();
  }
  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    if(_snapUpTimer != null){
      _snapUpTimer?.cancel();
      _snapUpTimer = null;
    }
  }

  void startCountdown() {
    _snapUpTimer = Timer.periodic(const Duration(seconds: 1), (Timer timer) {
      if (secondsRemaining > 0) {
        secondsRemaining--;
        int hours = secondsRemaining ~/ 3600;
        int minutes = (secondsRemaining % 3600) ~/ 60;
        int remainingSeconds = secondsRemaining % 60;

        String hoursStr = (hours < 10) ? '0$hours' : hours.toString();
        String minutesStr = (minutes < 10) ? '0$minutes' : minutes.toString();
        String secondsStr = (remainingSeconds < 10) ? '0$remainingSeconds' : remainingSeconds.toString();

        snapUpHour.value = hoursStr;
        snapUpMinute.value = minutesStr;
        snapUpSecond.value = secondsStr;
      } else {
        timer.cancel();
        snapUpHour.value = '00';
        snapUpMinute.value = '00';
        snapUpSecond.value = '00';
      }
    });
  }
  void test(int index){
    print('xxxxxxDDDDD $index');

    if(lastPage == index)return;
    lastPage = index;
    animateToOffset(scrollController, Get.width * lastPage, (){});

  }




  PointerDownEventListener getPointDownListenerInHorizontal() {
    return (event) {
      pointerStart = event.position;
    };
  }

  PointerUpEventListener getPointUpListenerInHorizontal() {

    return (event){
      pointerEnd = event.position;
      touchRangeX = pointerStart.dx - pointerEnd.dx;
      touchRangeY =pointerStart.dy - pointerEnd.dy;

      double screenWidth = Get.width;
      //所有的操作必须要满足滑动距离>10才算是滑动
      if (touchRangeX.abs() < 10) {
        nextOffset = screenWidth * lastPage;
        animateToOffset(scrollController, nextOffset, () {
          if (lastPage < 0) {
            lastPage = 0;
          }
        });
        return;
      }
      //纵向操作大于横向操作三倍视为纵向操作
      //这个判断拦截只有在纵向操作距离大于20.0的时候才生效
      if (touchRangeX.abs() < touchRangeY.abs() && touchRangeY > 20) {
        nextOffset = screenWidth * lastPage;
        animateToOffset(scrollController, nextOffset, () {
          if (lastPage < 0) {
            lastPage = 0;
          }
        });
        return;
      }

      //如果滑动小于当前屏幕1/8，那么就回弹复原，超过则移动到下一页
      //跳转到下一页或者上一页或者不动
      if (touchRangeX > screenWidth / 8) {
        nextOffset = screenWidth * (lastPage + 1);
        print("animate to ${nextOffset}");
        animateToOffset(scrollController, nextOffset, () {
          lastPage++;
          if (lastPage >= state.images.length - 1) {
            lastPage = state.images.length - 1;
          }
        });
      } else if (touchRangeX < -1 * screenWidth / 8) {
        nextOffset = screenWidth * (lastPage - 1);
        print("animate to ${nextOffset}");
        animateToOffset(scrollController, nextOffset, () {
          lastPage--;
          if (lastPage < 0) {
            lastPage = 0;
          }
        });
      } else {
        animateToOffset(scrollController, screenWidth * lastPage, (){});
      }
    };

  }

  void animateToOffset(ScrollController controller, double offset,
      void Function() onScrollCompleted) {
    controller
        .animateTo(offset,
        duration: Duration(milliseconds: 200), curve: Curves.easeIn)
        .then((value) {
      if (onScrollCompleted != null) {
        onScrollCompleted();
      }
    }).catchError((e) {
      print(e);
    });
  }
}
